<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <h1>冒泡排序</h1>
    <h2>冒泡排序的基本思想是：设排序序列的记录个数为n，进行n-1次遍历，每次遍历从开始位置依次往后比较前后相邻元素，这样较大的元素往后移，n-1次遍历结束后，序列有序</h2>

    <script>
        function bubbleSort (arr) {
            if (arr.length < 2) return arr;
            for (let i=0; i<arr.length; i++) {
                for (let j=i+1; j<arr.length; j++) {
                    if (arr[i] > arr[j]) {
                        let tmp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = tmp;
                    }
                }
            }
            return arr;
        }
        
        let res = bubbleSort([546,22,44,-45,52,24,54]);
        console.log(res);
    </script>
</body>

</html>